package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.Doctors;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.DoctorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doctors")
public class DoctorsController {
    @Autowired
    private DoctorsService doctorsService;
    /*
    * 1.获取所有医生信息
    * 2.添加医生
    * 3.更新医生信息
    * 4.医生离职操作
    * 5.根据医生名字模糊查询
    * */

    /*
    * 1.获取所有医生信息
    * */
    @GetMapping("/getDoctors")
    public Result getDoctorsList(){
        List<Doctors> doctorsList = doctorsService.getDoctersList();
        if(doctorsList!=null){
            return new Result(true,doctorsList);
        }else{
            return new Result(true,"请求成功，数据库中没有医生信息");
        }
    }

    /*
     * 2.添加医生
     * */
    @PostMapping("/addDoctor")
    public Result addDoctor(@RequestBody Map<String,Object> requestBody){
        String doctor_name = (String) requestBody.get("doctor_name");
        String doctor_gender = (String) requestBody.get("doctor_gender");
        String expertise = (String) requestBody.get("expertise");
        Integer years_of_experience = Integer.parseInt((String) requestBody.get("years_of_experience"));
        String contact_number = (String) requestBody.get("contact_number");
        int result = doctorsService.addDoctor(doctor_name, doctor_gender, expertise, years_of_experience, contact_number);
        if(result>0){
            return new Result(true,"添加成功");
        }else{
            return new Result(false,"添加失败");
        }

    }
    /*
     * 3.更新医生信息
     * */
    @PutMapping("/updateDoctor")
    public Result deleteUserById(@RequestBody Doctors doctor){
        int result = doctorsService.updateDoctor(doctor);
        if(result>0){
            return new Result(true,"修改成功");
        }else{
            return new Result(false,"修改失败");
        }
    }
    /*
     * 4.医生离职操作
     * */
    @GetMapping("/delDoctor/{doctor_id}")
    public Result delDoctor(@PathVariable Integer doctor_id){
        int result = doctorsService.delDoctor(doctor_id);
        if(result>0){
            return new Result(true,"操作成功");
        }else{
            return new Result(false,"操作失败");
        }
    }
    /*
     * 5.根据医生关键字模糊查询
     * */
    @GetMapping("/getDoctorByName/{docter_name}")
    public Result getUserByName(@PathVariable String docter_name){
        String newKeyWord = "%"+docter_name+"%";
        return new Result(true,doctorsService.getDoctorByName(newKeyWord),"访问到接口了，但是不知道有没有数据");
    }
}
